package com.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.health.pojo.Member;
import com.health.service.MemberService;
import com.health.vo.PageResult;
import com.health.vo.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;
    /**
     * 编辑会员
     */
    @PostMapping("/update")
    public Result update(@RequestBody Member member){
        int count=memberService.update(member);

        if (count>0){
            return new Result(true,"编辑会员成功");
        }

        return new Result(false,"编辑会员失败");
    }
    /**
     * 编辑会员-根据id查询会员数据回显
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        Member member=memberService.findById(id);

        return new Result(true,"查询会员成功",member);
    }
    /**
     * 添加会员
     */
    @PostMapping("/add")
    public Result add(@RequestBody Member member){
        int count=memberService.add(member);

        if (count>0){
            return new Result(true,"添加会员成功");
        }

        return new Result(false,"添加会员失败");
    }

    /**
     * 删除会员-拥有ROLE_ADMIN权限方可执行操作
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public  Result deleteById(@PathVariable Integer id){
        int count=memberService.deleteById(id);

        if (count>0){
            return new Result(true,"删除会员成功");
        }

        return new Result(false,"删除会员失败");
    }


    /**
     * 会员列表分页查询
     */
    @GetMapping("/findPage/{pageNow}/{pageSize}")
    public Result findPage(@PathVariable Integer pageNow,
                                       @PathVariable Integer pageSize,
                                       String query){
        PageResult<Member> pageResult=memberService.findPage(pageNow,pageSize,query);

        return new Result(true,"会员列表分页查询成功",pageResult);
    }
}
